package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    protected ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/potentialList")
    public String list(Model model, CustomerQueryObject qo) {
        qo.setStatus((long) Customer.STATUS_POTENTIAL);
        // 当前登录用户
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
// 不是超级管理员或者不是市场经理只能看自己跟踪的客户
        if (!(employee.isAdmin() || SecurityUtils.getSubject().hasRole("Market_Manager"))) {
            qo.setSellerId(employee.getId());
        }  // 是超级管理员和市场经理才查询所有销售人员，回显在页面
        // 若是普通市场专员都没有这个功能，就没浪费数据查询了
//        List<Employee> sellers = employeeService.listBySellerId(qo);
        List<Employee> sellers = employeeService.listByRoleSn(new String[]{Role.ROLE_SN_MARKET_MANAGER, Role.ROLE_SN_MARKET});
        model.addAttribute("sellers", sellers);
        PageInfo result = customerService.query(qo);
        model.addAttribute("pageInfo", result);
        model.addAttribute("qo", qo);
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.queryBysystemDictionarySn("job");
        List<SystemDictionaryItem> sources= systemDictionaryItemService.queryBysystemDictionarySn("source");
        List<SystemDictionaryItem> traces= systemDictionaryItemService.queryBysystemDictionarySn("trace");
        model.addAttribute("jobs", jobs);
        model.addAttribute("sources", sources);
        model.addAttribute("traces", traces);
        return "customer/potentialList";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        if (id != null) {
            model.addAttribute("customer", customerService.get(id));
        }
        return "customer/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult save(Customer customer) {
        try {
            if (customer.getId() != null) {
                customerService.update(customer);
            } else {
                customerService.save(customer);
            }
        } catch (Exception e) {
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "操作成功");
    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long cid, int status) {
        try {
                customerService.updateStatus(cid,status);
        } catch (Exception e) {
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "操作成功");
    }

}
